




function Updater(config) {
  console.log(321321)
  const defaultConfig = {
    interval: 1000, //interval which check for update, unit is milliseconds
    onCheckForUpdate(){},
    onUpdateReady(){},
  }
  this.config = Object.assign({}, defaultConfig, config)
  console.log(this.config)
  //single update manager instance of update mini-app
  this.updateManager = wx.getUpdateManager()
}

Updater.prototype = {
  start() {
    this.stop()
    this.itv = setInterval(() => {
      this.updateManager.onCheckForUpdate((res)=>{
        if(res.hasUpdate !== false) {
          this.config.onCheckForUpdate.call(this)
        }
      })
      this.updateManager.onUpdateReady((res)=>{
        this.config.onUpdateReady.call(this)
      })
    }, this.config.interval)
    return this.itv
  },
  stop() {
    if (this.itv) {
      clearInterval(this.itv)
      this.itv = null
    } 
  },
  applyUpdate() {
    this.updateManager.applyUpdate()
  }
}

let instance 

var  updater = function(config) {
  return instance || new Updater(config)
}

module.exports = exports = updater